// packageJ/phoneBillRecharge/record/record.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    total_page: 0,
    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      this.setData({
        ind: options.ind,
        id: options.id
      })
    }
    this.textChange();
    this.getData();
  },
  textChange() {
    let txt = "";
    if (this.data.ind == 0) {
      txt = "剩余未充值期数";
    } else if (this.data.ind == 1) {
      txt = "累计已返佣金";
    } else if (this.data.ind == 2) {
      txt = "剩余未返佣金";
    }
    wx.setNavigationBarTitle({
      title: txt,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let urlStr = ["plugin.phone-bill-recharge.frontend.return-balance-record.future", "plugin.phone-bill-recharge.frontend.return-balance-record.returned", "plugin.phone-bill-recharge.frontend.return-balance-record.future-periods"];
      let json = {
        return_balance_id: this.data.id,
        page:this.data.page
      };
      app._postNetWork({
        url: app.getNetAddresss(urlStr[this.data.ind]),
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: this.data.list.concat(res.data.data),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getData() {
    let urlStr = ["plugin.phone-bill-recharge.frontend.return-balance-record.future", "plugin.phone-bill-recharge.frontend.return-balance-record.returned", "plugin.phone-bill-recharge.frontend.return-balance-record.future-periods"];
    let json = {
      return_balance_id: this.data.id
    };
    app._getNetWork({
      url: app.getNetAddresss(urlStr[this.data.ind]),
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            isLoadMore: true,
            list: res.data.data,
            total_page: res.data.last_page
          })
          if (!this.data.total_page) {
            this.setData({
              total_page: 0
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res);
      },
    });
  }
})